// pages/mycoupon/mycoupon.js
const app = getApp();
var formatTime = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      { "title": "未使用", "num": 0 },
      { "title": "已使用", "num": 0 },
      { "title": "已过期", "num": 0 }
    ],
    activeIndex: 0, //当前选中的类型
    sliderOffset: 0, //滑块偏移距离
    sliderLeft: 10, //滑块初始left值
    couponList: [], //优惠券列表
    //为空提示
    emptyTip: {
      font: "这里空空如也~",
      imgUrl: "https://rybc.0791jr.com/WeChat/images/empty-ticket.png"
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.selectCoupon = options.selectCoupon;
  },

  //切换类型
  tabClick: function (e) {
    this.setData({
      //translateX的值就是终点目标距离左边的距离减去初始left的值
      sliderOffset: e.currentTarget.offsetLeft - 10,
      activeIndex: e.currentTarget.id
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const userId = app.globalData.userId;
    wx.request({
      url: app.baseURL + 'Mine&a=coupon',
      data: {
        user_id: userId
      },
      success: (res) => {
        console.log(res);

        let data = res.data;
        this.format(data.yhq_a, 'start_time');
        this.format(data.yhq_a, 'end_time');
        this.format(data.yhq_b, 'start_time');
        this.format(data.yhq_b, 'end_time');
        this.format(data.yhq_c, 'start_time');
        this.format(data.yhq_c, 'end_time');

        let tabs = this.data.tabs;
        tabs[0].num = data.yhq_a.length;
        tabs[1].num = data.yhq_b.length;
        tabs[2].num = data.yhq_c.length;
        this.setData({
          weishiyong: data.yhq_a,
          yishiyong: data.yhq_b, 
          yiguoqi: data.yhq_c,
          tabs: tabs
        })
      }
    })
  },

  //将时间戳转换成日期
  format(arr, prop) {
    arr.forEach((item) => {
      item[prop] = formatTime.formatTime(item[prop] * 1000, 'hms');
    });
  },

  //立即使用优惠券
  toUseCoupon() {
    console.log(1)
    wx.switchTab({
      url: '../classify/classify',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  }
})